﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;
using BE;
using BC;
using DAL;

namespace ModuleDuoc
{
    public partial class frmDonThuoc : ModuleDuoc.Base.frmBase
    {
        int _err;
        DataTable dt;
        string _manhanvien = "Nguyễn Văn Hiển";
        public frmDonThuoc()
        {
            InitializeComponent();
            this.pnlContent.Enabled = true;
        }
        public frmDonThuoc(string tennhanvien)
        {
            InitializeComponent();
            _manhanvien = tennhanvien;
        }
        public void GetValue(String str1)
        {
            // khai báo 1 hàm với tham số đầu vào là str1, nó sẽ đưa dữ liệu vào lable
            txtDelegate.Text = str1;
        }
        private void frmDonThuoc_Load(object sender, EventArgs e)
        {
            this.fillCboxSP();
            this.fillCboxSP_1();
            this.cboxLieuDung.Text = "Ngày uống 1 viên sáng sau ăn.";
            this.lblTitle.Text = "ĐƠN THUỐC";
            this.txtBacSi.Text = _manhanvien;
            this.cboxGioiTinh.Text = "Nam";
            this.loadSoPhieu();
        }
        private void loadSoPhieu()
        {
            //string sql = "SELECT sophieu FROM zD_PHIEUXUATKHO ";manhanvien = N'Nguyễn Văn Hiển'
            string sql = "SELECT donthuocID, ngaylap FROM zD_DONTHUOC WHERE manhanvien = N'" +_manhanvien+ "' AND ngaylap BETWEEN '" + this.date1.Value.ToString("MM/dd/yyyy 00:00:00") + "' AND '" + this.date2.Value.ToString("MM/dd/yyyy 23:59:59") + "' ORDER BY ngaylap desc,donthuocid";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            this.dgvLeft.DataSource = dt;
            this.btnSoLuong.Text = "Số lượng:" + dt.Rows.Count;
        }
        private void setTab(Control c, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                c.Focus();
            }
        }
        private void getInfoSID()
        {
            try
            {
                //string sql = "select * from hien_BanLe where sophieu='" + this.txtSoPhieu.Text.Trim() + "'";
                string s = this.dateTimeSID.Text.Replace("/", "").Substring(0, 4) + this.dateTimeSID.Text.Replace("/", "").Substring(6, 2) + "-" + this.txtSID.Text;
                string sql = "SELECT PatientName, Sex, Age, [Address], Phone, PID FROM tbl_Patient WHERE [SID] ='" + s + "'";
                dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString01"], CommandType.Text, sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    this.txtTenKH.Text = dt.Rows[0]["PatientName"].ToString();
                    this.txtDiaChi.Text = dt.Rows[0]["Address"].ToString();
                    this.txtTuoi.Text = dt.Rows[0]["Age"].ToString();
                    this.txtDienThoai.Text = dt.Rows[0]["Phone"].ToString();
                    this.txtPID.Text = dt.Rows[0]["PID"].ToString();

                    if (dt.Rows[0]["Sex"].ToString() == "M")
                    {
                        this.cboxGioiTinh.Text = "Nam";
                    }
                    else
                    {
                        this.cboxGioiTinh.Text = "Nữ";
                    }
                    this.txtKetLuan.Focus();
                }
                this.txtTenKH.Focus();
            }
            catch (Exception)
            { }
        }

        private void txtTuoi_TextChanged(object sender, EventArgs e)
        {
            if (this.txtTuoi.Text.Length == 2)
            {
                try
                {
                    this.lblNamSinh.Text = (DateTime.Now.Year - Convert.ToInt32(this.txtTuoi.Text)).ToString();
                }
                catch (Exception)
                { }
            }
            else if (this.txtTuoi.Text.Length == 4)
            {
                this.lblNamSinh.Text = this.txtTuoi.Text;
            }
        }

        private void cboxSP_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtSoLuong.Text = "";
            if (this.cboxSP.Text != "")
            {
                BC.ZD_DM_THUOCBC bc = new BC.ZD_DM_THUOCBC();
                dt = new DataTable();
                try
                {
                    dt = bc.Search(this.cboxSP.Text.ToString().Trim());
                    if (dt.Rows.Count > 0)
                    {
                        this.txtDVT.Text = dt.Rows[0]["ten"].ToString();
                        //this.txtLieuDung.Text = dt.Rows[0]["Usage"].ToString();
                        this.cboxLieuDung.Text = dt.Rows[0]["Usage"].ToString();
                        this.cboxHoatChat.Text = dt.Rows[0]["Infoinclude"].ToString();
                        //this.txtGiaBan.Text = dt.Rows[0]["GiaBan"].ToString();
                        //this.txtVAT.Text = dt.Rows[0]["Vat"].ToString();
                    }
                    else
                    {
                        //this.txtDVT.Text = "";
                        this.cboxLieuDung.Text = "";
                    }
                }
                catch (Exception)
                { }
            }
        }
        
        private void cboxBietDuoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.radioButton2.Checked == true)
            {
                this.txtSoLuong.Text = "";
                if (this.cboxHoatChat.Text != "")
                {
                    BC.ZD_DM_THUOCBC bc = new BC.ZD_DM_THUOCBC();
                    dt = new DataTable();
                    try
                    {
                        dt = bc.Search_BietDuoc(this.cboxHoatChat.Text.ToString().Trim());
                        if (dt.Rows.Count > 0)
                        {
                            //this.txtDVT.Text = dt.Rows[0]["ten"].ToString();
                            this.cboxLieuDung.Text = dt.Rows[0]["Usage"].ToString();
                            this.cboxSP.Text = dt.Rows[0]["tenvt"].ToString();
                            this.cboxSP.DataSource = dt;
                            //this.cboxSP.Focus();
                            this.cboxSP.DisplayMember = "TenVT";
                            this.cboxSP.ValueMember = "MaVT";

                        }
                        else
                        {
                            this.txtDVT.Text = "";
                            this.cboxLieuDung.Text = "";
                        }
                    }
                    catch (Exception)
                    { }
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (checkInput() == true & checkSP_DGV() == true)
            {
                try
                {
                    this.dgvData.Rows.Add(this.cboxSP.Text + " (" + this.cboxHoatChat.Text + ")", this.txtDVT.Text, this.txtSoLuong.Text, this.cboxLieuDung.Text);

                    if (this.txtMaDon.Text != "")// them truc tiep
                    {
                        int i = this.dgvData.Rows.Count - 1;
                        ZD_DONTHUOCCTBC bc = new ZD_DONTHUOCCTBC();
                        ZD_DONTHUOCCT obj = new ZD_DONTHUOCCT();
                        obj.DonThuocId = Int32.Parse(this.dgvLeft.Rows[this.dgvLeft.CurrentRow.Index].Cells["donthuocID"].Value.ToString());
                        //obj.Mavt = this.dgvData.Rows[i].Cells["mavt"].Value.ToString();
                        //obj.Mavach = Int64.Parse(this.dgvData.Rows[i].Cells["mavach"].Value.ToString());
                        obj.Tenvt = this.dgvData.Rows[i].Cells["tenvt"].Value.ToString();
                        obj.Soluong = Int32.Parse(this.dgvData.Rows[i].Cells["soluong"].Value.ToString());
                        obj.Dvt = this.dgvData.Rows[i].Cells["dvt"].Value.ToString();
                        obj.LieuDung = this.dgvData.Rows[i].Cells["lieuDung"].Value.ToString();

                        bc.Insert(obj);
                        getDonBS();
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Giá nhập không hợp lệ!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                //this.cboxSP.Focus();
            }

        }


        private bool checkInput()
        {
            if (this.cboxSP.Text == "")
            {
                MessageBox.Show("Tên thuốc không hợp lệ!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtTenKH.Focus();
                return false;
            }
            else if (this.txtSoLuong.Text == "")
            {
                MessageBox.Show("Số lượng không hợp lệ!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //this.txtSoLuong.Focus();
                return false;
            }
            else if (this.cboxLieuDung.Text == "")
            {
                MessageBox.Show("Liều dùng không hợp lệ!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.cboxLieuDung.Focus();
                return false;
            }
            return true;
        }


        private bool checkSP_DGV()
        { 
            for(int i = 0; i<this.dgvData.Rows.Count; i++)
            {
                //string _tensp = this.dgvData.Rows[dgvData.CurrentRow.Index].Cells["tensp"].Value.ToString().Substring(0, this.dgvData.Rows[dgvData.CurrentRow.Index].Cells["tensp"].Value.ToString().IndexOf("("));
                if (this.cboxSP.Text + "(" + this.cboxHoatChat.Text + ")" == this.dgvData.Rows[i].Cells[0].Value.ToString())
                {
                    MessageBox.Show("Thuốc này đã được kê ở dưới rồi!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    //this.cboxSP.Focus();
                    return false;
                }
            }
            return true;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //if (this.txtMaDon.Text != "")
            {
                if (MessageBox.Show("Bạn có muốn xóa thuốc này không?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        ZD_DONTHUOCCTBC bc = new ZD_DONTHUOCCTBC();
                        bc.Delete(ZD_DONTHUOCCT.ZD_DONTHUOCCTFields.Id, this.dgvData.Rows[this.dgvData.CurrentRow.Index].Cells["id"].Value.ToString());
                        //MessageBox.Show("Xóa thành công !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception)
                    { }
                    for (int i = this.dgvData.SelectedRows.Count - 1; i >= 0; i--)
                    {
                        this.dgvData.Rows.RemoveAt(this.dgvData.SelectedRows[i].Index);
                    } 
                }
            }      
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "[0123456789]") && !Char.IsControl(e.KeyChar))
                e.Handled = true;

        }

        private void txtTuoi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "[0123456789]") && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txtDienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "[0123456789]") && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        #region Click buttom


        private void clear()
        {
            groupBox1.Enabled = true;
            groupBox2.Enabled = true;
            dgvData.Enabled = true;
            this.txtSID.Text = "";
            this.txtPID.Text = "";
            this.txtTenKH.Text = "";
            this.txtDiaChi.Text = "";
            this.txtDienThoai.Text = "";
            this.txtTuoi.Text = "";
            this.lblNamSinh.Text = "0";
            this.txtBacSi.Text = _manhanvien;
            this.txtKetLuan.Text = "";
            this.txtLoiDan.Text = "";
            this.dgvData.Rows.Clear();
            this.txtMaDon.Text = "";
            this.dateTimeSID.Focus();
            this.loadSoPhieu();
        }

        public override void btnSave_Click(object sender, EventArgs e)
        {
            base.btnSave_Click(sender, e);
            if (this.groupBox1.Enabled == true && this.groupBox2.Enabled == true)
            {
                if (dgvData.Rows.Count > 0)
                {
                    if (this.txtTenKH.Text == "")
                    {
                        MessageBox.Show("Tên khách hàng không hợp lệ!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtSID.Focus();
                    }
                    else if (this.txtKetLuan.Text.Length > 400)
                    {
                        MessageBox.Show("Kết luận bệnh vượt quá kí tự cho phép!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtKetLuan.Focus();
                    }
                    else if (this.txtLoiDan.Text.Length > 200)
                    {
                        MessageBox.Show("Lời dặn vượt quá kí tự cho phép!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtLoiDan.Focus();
                    }
                    else
                    {
                        this.checkSL();
                        if (_err == 0)
                        {
                            if (this.txtMaDon.Text == "")
                            {
                                this.insertDonThuoc();

                                this.dgvData.Rows.Clear();
                                this.clear();
                            }
                            else
                            {
                                this.updateDonThuoc();
                                this.dgvData.Rows.Clear();
                                this.clear();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Số lượng thuốc không hợp lệ!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Không có dữ liệu!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.cboxSP.Focus();
                }
            }
        }
        private int checkSL()
        {
            _err = 0;
            for (int i = 0; i < dgvData.Rows.Count; i++)
            {
                try
                {
                    Int32.Parse(this.dgvData.Rows[i].Cells["soluong"].Value.ToString());
                }
                catch (Exception)
                {
                    return _err++;
                }
            }
            return _err;
        }

        #endregion

        private void fillCboxSP()
        {
            try
            {
                BC.ZD_DM_THUOCBC bc = new BC.ZD_DM_THUOCBC();
                this.cboxSP.DataSource = bc.GetAll();
                this.cboxSP.DisplayMember = "TenVT";
                this.cboxSP.ValueMember = "MaVT";
                //MessageBox.Show(this.cboxSP.SelectedValue.ToString());
               

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //Infoinclude

        private void fillCboxSP_1()
        {
            try
            {
                BC.ZD_DM_THUOCBC bc = new BC.ZD_DM_THUOCBC();
                this.cboxHoatChat.DataSource = bc.GetAll();
                this.cboxHoatChat.DisplayMember = "Infoinclude";
                this.cboxHoatChat.ValueMember = "MaVT";
                //MessageBox.Show(this.cboxSP.SelectedValue.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void insertDonThuoc()
        {
            try
            {
                BE.ZD_DONTHUOC be = new BE.ZD_DONTHUOC();
                BC.ZD_DONTHUOCBC bc = new ZD_DONTHUOCBC();

                be.Manhanvien = this.txtBacSi.Text;
                be.Ngaylap = DateTime.Now;
                be.Tenmay = "10.0.0.5";
                be.Xuathang = false;
                //be.Ngayban = DateTime.Now;
                be.Printed = 0;
                //be.PrintedDate = DateTime.Now;
                if (this.txtSID.Text.Length == 7)
                {
                    be.SID = this.txtSID.Text;
                }
                else
                {
                    be.SID = "";
                }
                be.LoiDan = this.txtLoiDan.Text;
                be.KetLuanBenh = this.txtKetLuan.Text;
                be.DateSID = dateTimeSID.Value;
                be.TenKH = this.txtTenKH.Text;
                be.DiaChiKH = this.txtDiaChi.Text;
                be.TuoiKH = Int32.Parse(this.lblNamSinh.Text);
                if (this.cboxGioiTinh.Text == "Nam")
                {
                    be.GioiTinhKH = true;
                }
                else
                {
                    be.GioiTinhKH = false;
                }
                be.DienThoaiKH = this.txtDienThoai.Text;
                bc.Insert(be);
                insertDonThuocCT();
                MessageBox.Show("Thêm mới đơn thuốc thành công!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                //this.pnlContent.Enabled = false;
                this.printReport();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi thêm đơn thuốc!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show(ex.StackTrace);
            }
        }
        private void updateDonThuoc()
        {
            try
            {
                BE.ZD_DONTHUOC be = new BE.ZD_DONTHUOC();
                BC.ZD_DONTHUOCBC bc = new ZD_DONTHUOCBC();

                be.DonThuocId = Int32.Parse(this.txtMaDon.Text);
                be.Manhanvien = this.txtBacSi.Text;
                be.Ngaylap = DateTime.Parse(this.dgvLeft.Rows[this.dgvLeft.CurrentRow.Index].Cells["ngaylap"].Value.ToString());//DateTime.Now;
                be.Tenmay = "10.0.0.5";
                be.Xuathang = false;
                //be.Ngayban = DateTime.Now;
                be.Printed = 0;
                //be.PrintedDate = DateTime.Now;
                if (this.txtSID.Text.Length == 7)
                {
                    be.SID = this.txtSID.Text;
                }
                else
                {
                    be.SID = "";
                }
                be.LoiDan = this.txtLoiDan.Text;
                be.KetLuanBenh = this.txtKetLuan.Text;
                be.DateSID = dateTimeSID.Value;
                be.TenKH = this.txtTenKH.Text;
                be.DiaChiKH = this.txtDiaChi.Text;
                be.TuoiKH = Int32.Parse(this.lblNamSinh.Text);
                if (this.cboxGioiTinh.Text == "Nam")
                {
                    be.GioiTinhKH = true;
                }
                else
                {
                    be.GioiTinhKH = false;
                }
                be.DienThoaiKH = this.txtDienThoai.Text;
                bc.Update(be);
                //updateDonThuocCT();
                MessageBox.Show("Sửa đơn thuốc thành công!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                //this.pnlContent.Enabled = false;
                //this.printReport();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi sửa đơn thuốc!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show(ex.StackTrace);
            }
        }
        private void insertDonThuocCT()
        {
            try
            {
                BE.ZD_DONTHUOCCT be = new BE.ZD_DONTHUOCCT();
                BC.ZD_DONTHUOCCTBC bc = new ZD_DONTHUOCCTBC();
                dt = new DataTable();

                BC.ZD_DONTHUOCBC bc1 = new ZD_DONTHUOCBC();
                DataTable dt1 = new DataTable();
                dt1 = bc1.Get_Check_ThuocID("10.0.0.5");

                for (int i = 0; i < this.dgvData.Rows.Count; i++)
                {
                    be.DonThuocId = Int32.Parse(dt1.Rows[0][0].ToString());
                    be.Mavt = this.cboxSP.SelectedValue.ToString();
                    be.Tenvt = this.dgvData.Rows[i].Cells[0].Value.ToString();
                    be.Soluong = Int32.Parse(this.dgvData.Rows[i].Cells[2].Value.ToString());
                    be.Dvt = this.dgvData.Rows[i].Cells[1].Value.ToString();
                    be.LieuDung = this.dgvData.Rows[i].Cells[3].Value.ToString();
                    bc.Insert(be);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi thêm chi tiết đơn thuốc!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void printReport()
        {
            try
            {
                BC.ZD_DONTHUOCBC bc1 = new ZD_DONTHUOCBC();
                DataTable dt1 = new DataTable();
                dt1 = bc1.Get_Check_ThuocID("10.0.0.5");

                string sql = "select * from hien_rpt_DonThuoc where donThuocID='" + dt1.Rows[0][0].ToString() + "'";
                DataTable dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
                frmShowReport frm = new frmShowReport(3);
                frmShowReport._dt = dt;
                frmShowReport._nhanvien = _manhanvien;
                frmShowReport._tenKH = this.txtTenKH.Text;
                frmShowReport._tuoiKH = this.txtTuoi.Text;
                frmShowReport.strC = frmShowReport.BC.DONTHUOC;
                frm.ShowDialog();
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi hiển thị report!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtSID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "[0123456789]") && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        //private void txtLieuDung_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyCode == Keys.Enter)
        //    {
        //        btnAdd_Click(sender, e);
        //        if (this.radioButton1.Checked == true)
        //        {
        //            setTab(this.cboxSP, e);
        //        }
        //        else if (this.radioButton2.Checked == true)
        //        {
        //            setTab(this.cboxHoatChat, e);
        //        }
        //    }
        //}

        private void dgvData_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                Int32.Parse(this.dgvData.Rows[this.dgvData.CurrentRow.Index].Cells["soluong"].Value.ToString());
            }
            catch (Exception)
            {
                MessageBox.Show("Số lượng nhập vào không hợp lệ!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //1
        private void txtSID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.txtMaDon.Text == "")
                {
                    try
                    {
                        if (this.txtSID.Text.Substring(0, 1) == "0")
                        {
                            this.txtSID.Text = this.txtSID.Text.Substring(1);
                        }
                    }
                    catch (Exception)
                    { }
                    if (this.txtSID.Text.Length == 7)
                    {
                        this.getInfoSID();//26092012
                        setTab(this.txtKetLuan, e);
                    }
                    else
                    {
                        // this.txtSID.Text = "";
                        this.txtTenKH.Text = "";
                        this.txtDiaChi.Text = "";
                        this.txtDienThoai.Text = "";
                        this.cboxGioiTinh.Text = "Nam";
                        setTab(this.txtTenKH, e);
                    }
                }
            }
        }

        private void getKHbyDate()
        {
            try
            {
                if (this.txtSID.Text.Substring(0, 1) == "0")
                {
                    this.txtSID.Text = this.txtSID.Text.Substring(1);
                }
            }
            catch (Exception)
            { }
            if (this.txtSID.Text.Length == 7)
            {
                this.getInfoSID();//26092012
            }
            else
            {
                this.txtTenKH.Text = "";
                this.txtDiaChi.Text = "";
                this.txtDienThoai.Text = "";
                //setTab(this.dateTimeSID, e);
            }
            //setTab(this.txtPID, e);
        }

        private void txtKetLuan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                if (this.radioButton1.Checked == true)
                {
                    setTab(this.cboxSP, e);
                }
                else if (this.radioButton2.Checked == true)
                {
                    setTab(this.cboxHoatChat, e);
                }
            }
        }

        private void cboxSP_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setTab(this.txtSoLuong, e);
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            this.fillCboxSP();
            this.cboxSP.Focus();
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            this.cboxHoatChat.Focus();
        }

        private void dateTimeSID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setTab(this.txtSID, e);
            }
        }

        private void cboxHoatChat_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setTab(this.cboxSP, e);
            }
        }

        private void dateTimeSID_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.txtSID.Text.Substring(0, 1) == "0")
                {
                    this.txtSID.Text = this.txtSID.Text.Substring(1);
                }
            }
            catch (Exception)
            { }
            if (this.txtSID.Text.Length == 7)
            {
                this.getInfoSID();//26092012
                //setTab(this.txtMaVach, e);
            }
            else
            {
                this.txtTenKH.Text = "";
                this.txtDiaChi.Text = "";
                this.txtDienThoai.Text = "";
                //setTab(this.txtTenKH, e);
            }
        }

        private void txtPID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setTab(this.txtTenKH, e);
            }
        }

        private void txtTenKH_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setTab(this.txtDiaChi, e);
            }
        }

        private void txtTuoi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setTab(this.cboxGioiTinh, e);
            }
        }

        private void cboxGioiTinh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setTab(this.txtDienThoai, e);
            }
        }

        private void txtDienThoai_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setTab(this.txtKetLuan, e);
            }
        }

        private void txtDiaChi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setTab(this.txtTuoi, e);
            }
        }

        private void dgvLeft_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.groupBox1.Enabled = false;
            this.panel4.Enabled = false;
            this.groupBox2.Enabled = false;
            this.dgvData.Enabled = false;
            getDonThuocBS();
            getDonBS();
        }

        private void getDonBS()
        {
            this.dgvData.Rows.Clear();
            try
            {
                string sql = "select * FROM zD_DONTHUOCCT where donthuocid = '" + this.dgvLeft.Rows[this.dgvLeft.CurrentRow.Index].Cells["donthuocID"].Value.ToString() + "'";
                DataTable dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        this.dgvData.Rows.Add(dt.Rows[i]["tenvt"].ToString(), dt.Rows[i]["dvt"].ToString(), dt.Rows[i]["soluong"].ToString(), dt.Rows[i]["lieudung"].ToString(), dt.Rows[i]["id"].ToString());
                    }
                }
            }
            catch (Exception)
            {
            }
        }
        private void getDonThuocBS()
        {
            this.txtKetLuan.Text = "";
            this.txtLoiDan.Text = "";
            //int id = 28;// 
            try
            {
                string sql = "select * from zD_DONTHUOC where donthuocid='" + this.dgvLeft.Rows[this.dgvLeft.CurrentRow.Index].Cells["donthuocID"].Value.ToString() + "'";
                DataTable dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
                this.txtMaDon.Text = dt.Rows[0]["donthuocID"].ToString();
                this.txtBacSi.Text = dt.Rows[0]["manhanvien"].ToString();
                this.txtSID.Text = dt.Rows[0]["SID"].ToString();
                this.dateTimeSID.Text = dt.Rows[0]["dateSID"].ToString();
                this.txtTenKH.Text = dt.Rows[0]["tenKH"].ToString();
                this.txtDiaChi.Text = dt.Rows[0]["diachiKH"].ToString();
                this.txtTuoi.Text = dt.Rows[0]["tuoiKH"].ToString();
                this.txtDienThoai.Text = dt.Rows[0]["dienthoaiKH"].ToString();
                if (bool.Parse(dt.Rows[0]["gioiTinhKH"].ToString()) == true || dt.Rows[0]["gioiTinhKH"].ToString() == "")
                {
                    this.cboxGioiTinh.Text = "Nam";
                }
                else
                {
                    this.cboxGioiTinh.Text = "Nữ";
                }
                this.txtKetLuan.Text = dt.Rows[0]["ketLuanBenh"].ToString();
                this.txtLoiDan.Text = dt.Rows[0]["loiDan"].ToString();
            }
            catch (Exception)
            {
                //this.dgvData.Rows.Clear();
                //this.txtBacSi.Text = "";
                //this.txtSID.Text = "";
            }
        }
        public override void btnEdit_Click(object sender, EventArgs e)
        {
            base.btnEdit_Click(sender, e);
            //if (this.grpDonMau.Visible == false)
            {
                if (this.txtBacSi.Text == _manhanvien)
                {
                    this.groupBox1.Enabled = true;
                    this.groupBox2.Enabled = true;
                    this.panel4.Enabled = true;
                    this.dgvData.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Người dùng không được sửa đơn thuốc của người khác!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        public override void btnInPhieu_Click(object sender, EventArgs e)
        {
            base.btnInPhieu_Click(sender, e);

            //if (this.grpDonMau.Visible == false)
            {
                try
                {
                    string sql = "select * from hien_rpt_DonThuoc where donThuocID='" + dgvLeft.Rows[dgvLeft.CurrentRow.Index].Cells["donthuocID"].Value.ToString() + "'";
                    DataTable dt = new DataTable();
                    dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
                    frmShowReport frm = new frmShowReport(3);
                    frmShowReport._dt = dt;
                    frmShowReport._nhanvien = _manhanvien;
                    frmShowReport._tenKH = this.txtTenKH.Text;
                    frmShowReport._tuoiKH = this.txtTuoi.Text;
                    frmShowReport.strC = frmShowReport.BC.DONTHUOC;
                    frm.ShowDialog();
                }
                catch (Exception)
                {
                    MessageBox.Show("Lỗi hiển thị report!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.loadSoPhieu();
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //string sql = "SELECT distinct donthuocid, ngaylap FROM hien_SearchDonThuoc WHERE tenkh LIKE N'" + "%" + this.txtSearch.Text + "%' OR diachikh LIKE N'" + "%" + this.txtSearch.Text + "%' OR tenvt LIKE N'" + "%" + this.txtSearch.Text + "%' AND ngaylap BETWEEN '" + this.date1.Value.ToString("MM/dd/yyyy 00:00:00") + "' AND '" + this.date2.Value.ToString("MM/dd/yyyy 23:59:59") + "' GROUP BY DonThuocID, ngaylap ORDER BY donThuocID desc,ngaylap";
                string sql = "SELECT distinct donthuocid, ngaylap FROM hien_SearchDonThuoc WHERE ngaylap BETWEEN '" + this.date1.Value.ToString("MM/dd/yyyy 00:00:00") + "' AND '" + this.date2.Value.ToString("MM/dd/yyyy 23:59:59") + "'AND (tenkh LIKE N'" + "%" + this.txtSearch.Text + "%' OR diachikh LIKE N'" + "%" + this.txtSearch.Text + "%' OR tenvt LIKE N'" + "%" + this.txtSearch.Text + "%') GROUP BY DonThuocID, ngaylap ORDER BY donThuocID desc,ngaylap";
                DataTable dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
                this.dgvLeft.DataSource = dt;
                this.btnSoLuong.Text = "Số lượng:" + dt.Rows.Count;
            }
        }

        private void cboxLieuDung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnAdd_Click(sender, e);
                if (this.radioButton1.Checked == true)
                {
                    setTab(this.cboxSP, e);
                }
                else if (this.radioButton2.Checked == true)
                {
                    setTab(this.cboxHoatChat, e);
                }
            }
        }

        private void txtSoLuong_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setTab(this.cboxLieuDung, e);
            }
        }

        private void dgvLeft_KeyDown(object sender, KeyEventArgs e)
        {
            getDonThuocBS();
            getDonBS();
        }

        private void dgvLeft_KeyUp(object sender, KeyEventArgs e)
        {
            getDonThuocBS();
            getDonBS();
        }

        public override void btnHuyPhieu_Click(object sender, EventArgs e)
        {
            base.btnHuyPhieu_Click(sender, e);
            if (this.txtMaDon.Text != "")
            {
                if (MessageBox.Show("Bạn có muốn xóa đơn thuốc này không?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        for (int i = 0; i < this.dgvData.Rows.Count; i++)
                        {
                            ZD_DONTHUOCCTBC bc = new ZD_DONTHUOCCTBC();
                            bc.Delete(ZD_DONTHUOCCT.ZD_DONTHUOCCTFields.Id, this.dgvData.Rows[i].Cells["id"].Value.ToString());
                        }
                        ZD_DONTHUOCBC bc1 = new ZD_DONTHUOCBC();
                        bc1.Delete(ZD_DONTHUOC.ZD_DONTHUOCFields.DonThuocId, this.dgvLeft.Rows[dgvLeft.CurrentRow.Index].Cells["DonThuocID"].Value.ToString());
                    }
                    catch (Exception)
                    { }
                    this.dgvData.Rows.Clear();
                    MessageBox.Show("Xóa thành công!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.loadSoPhieu();
                }
            } 
        }

        private void btnDonMau_Click(object sender, EventArgs e)
        {
            DM.DM_DonMau dm = new ModuleDuoc.DM.DM_DonMau();
            // chuyển  GetValue của form1 vào form2
            dm.MyGetData = new ModuleDuoc.DM.DM_DonMau.GetString(GetValue);
            dm.ShowDialog();           
        }

        private void insertDonMauCT()
        {
            try
            {
                BE.ZD_DonMauCT be = new BE.ZD_DonMauCT();
                BC.ZD_DonMauCTBC bc = new ZD_DonMauCTBC();
                DataTable dt = new DataTable();

                for (int i = 0; i < this.dgvData.Rows.Count; i++)
                {
                    be.DonMau_ID = Int32.Parse(getLast_DonMauID());
                    be.TenVT = this.dgvData.Rows[i].Cells[0].Value.ToString();
                    be.SoLuong = Int32.Parse(this.dgvData.Rows[i].Cells[2].Value.ToString());
                    be.DonViTinh = this.dgvData.Rows[i].Cells[1].Value.ToString();
                    be.LieuDung = this.dgvData.Rows[i].Cells[3].Value.ToString();
                    bc.Insert(be);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi thêm chi tiết đơn thuốc!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private string getLast_DonMauID()
        {
            string sql = "SELECT TOP 1 DonMau_ID FROM zD_DonMau zdm ORDER BY zdm.DonMau_ID desc";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            return dt.Rows[0]["DonMau_ID"].ToString();
        }

        private void txtDelegate_TextChanged(object sender, EventArgs e)
        {
            //MessageBox.Show(txtDelegate.Text);
            this.dgvData.Rows.Clear();
            string sql = "SELECT * FROM zD_DonMauCT zdmc WHERE zdmc.DonMau_ID = '"+ this.txtDelegate.Text +"'";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.dgvData.Rows.Add(dt.Rows[i]["TenVT"].ToString(), dt.Rows[i]["DonViTinh"].ToString(), dt.Rows[i]["SoLuong"].ToString(), dt.Rows[i]["LieuDung"].ToString());
            }
        }

        private void txtSearch_MouseClick(object sender, MouseEventArgs e)
        {
            this.txtSearch.Text = "";
        }
        public override void btnThemMoi_Click(object sender, EventArgs e)
        {
            base.btnThemMoi_Click(sender, e);
            this.txtBacSi.Text = _manhanvien;
            base.btnThemMoi_Click(sender, e);
            this.clear();
            //this.txtId.Text = "";
            //this.itmSua.Visible = false;
        }
    }
} 

